<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>登陆</title>

    <!--在线引用JQuery-->
    <script src="https://code.jquery.com/jquery-3.1.1.js"></script>
    <!-- 最新版本的 Bootstrap 核心 CSS 文件 -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css"
          integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">

    <!-- 可选的 Bootstrap 主题文件（一般不用引入） -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap-theme.min.css"
          integrity="sha384-6pzBo3FDv/PJ8r2KRkGHifhEocL+1X2rVCTTkUfGk7/0pbek5mMa1upzvWbrUbOZ" crossorigin="anonymous">

    <!-- 最新的 Bootstrap 核心 JavaScript 文件 -->
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"
            integrity="sha384-aJ21OjlMXNL5UyIl/XNwTMqvzeRMZH2w8c5cRVpzpU8Y5bApTppSuUkhZXN0VxHd"
            crossorigin="anonymous"></script>

    <script>
        //Ajax提交表单
        function loginFunction() {
            let userName = $('#userName').val();
            let userPassword = $('#userPassword').val();
            let rememberMe = $('rememberMe').is(':checked');
            if (userName.length < 4) {
                $('#userName').val('');
                $('#userPassword').val('');
                alert('您的账户名长度小于4位');
                return;
            }
            if (userPassword.length < 4) {
                $('#userName').val('');
                $('#userPassword').val('');
                alert('您的密码长度小于4位');
                return;
            }
            $.ajax({
                url: '/ajax/login',
                type: 'post',
                dataType: 'json',
                data: {
                    'userName': userName,
                    'userPassword': userPassword,
                    'rememberMe': rememberMe
                },
                success: function (result) {
                    $(result).each(function (index, map) {
                        //登陆返回结果
                        const loginMsg = map.login_msg;
                        if (loginMsg === '登陆成功') {
                            window.location.href = '/';
                        } else {
                            $('#userName').val('');
                            $('#userPassword').val('');
                            alert(loginMsg);
                            $.error("账户名或密码错误!!!");
                        }
                    });
                },
                error: function (result) {
                    console.log(result.toString());
                }
            });
        }
    </script>

    <style>
        #loginForm {
            border: 1px solid black;
            width: 400px;
            height: 200px;
            margin: auto;
        }

        #loginS {
            text-align: center;
        }
    </style>
</head>
<body>
<!--导航条-->
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">个人图标</a>
        </div>


        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li><a th:href="@{/}">首页<span class="sr-only">(current)</span></a></li>
                <li><a href="#">咸鱼广场</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">咸鱼工作<span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">IT</a></li>
                        <li><a href="#">金融</a></li>
                        <li><a href="#">服务</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">销售</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">土木</a></li>
                    </ul>
                </li>
            </ul>
            <form class="navbar-form navbar-left">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="天安门招聘保安,熟手优先">
                </div>
                <button type="submit" class="btn btn-default">Get it</button>
            </form>

            <ul class="nav navbar-nav navbar-right">
                <li class="active"><a th:href="@{/ajax/toLogin}" th:if="${userName==null}">登陆</a></li>
                <li><a th:href="@{/ajax/exit}" th:if="${userName!=null}">注销</a></li>
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">我的账户 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="#">Action</a></li>
                        <li><a href="#">Another action</a></li>
                        <li><a href="#">Something else here</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#">Separated link</a></li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<p align="center">欢迎登陆!</p>
<form id="loginForm" onsubmit="return false" action="#" method="post">
    <br><br>
    <div id="loginS">
        用户名 ：<label for="userName"></label><input id="userName" type="text" name="userName" required="required"><br><br>

        密码&nbsp;&nbsp;&nbsp;&nbsp;：<label for="userPassword"></label><input id="userPassword" type="password"
                                                                            name="userPassword"
                                                                            required="required"><br><br>
        <button id="loginBtn" type="button" onclick="loginFunction()">登陆</button>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <label for="rememberMe"></label><input type="checkbox" id="rememberMe" name="rememberMe">记住我?
    </div>
</form>
</body>
</html>